﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.MODEL.PrintManage;
using SmallERP.DAL;
using SmallERP.COM;
using SmallERP.Bll.Com;
using SmallERP.MODEL.FoundationModel;
using Smallerp.Contracts;
using System.Data;

namespace SmallERP.Bll.PrintManage
{

    public class PaperBLL : IDataAccess
    {
        IOperateDB _OperateDb;
        public PaperBLL(IOperateDB inOP)
        {

            _OperateDb = inOP;
        }
        public PaperInfoModel GetPaper(int paperId)
        {
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    PaperInfo paper = entities.PaperInfo.FirstOrDefault(p => p.PrintPaperId==paperId);
            //    PaperInfoModel paperInfo = MappingPaperToModel(paper);
            //    return paperInfo;
            //}
            string QueryPaper = "select AddTime,EndPaperLength,LastBordId,LastLength,LastPrintTime,Length,paperface.PaperFaceID,PaperFaceName,papertype.PaperTypeID,PaperTypeName,PrinterID,PrintPaperId,Remark,Status,InvalidationArea,ValidationArea,PaperConsumptionLength,Width from paperinfo,papertype,paperface where PrintPaperId=" + paperId + " and papertype.papertypeid=paperinfo.papertypeid and paperface.paperfaceid=paperinfo.paperfaceid";
            DataTable table=_OperateDb.QueryDataTable(QueryPaper);
            if (table.Rows.Count > 0)
                return MappingPaperToModel(table.Rows[0]);
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 获得正在使用状态的打印纸
        /// </summary>
        /// <returns></returns>
        public PaperInfoModel GetUsingPaper(int printerid)
        {
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    PaperInfo paper = entities.PaperInfo.FirstOrDefault(p => p.Status == (int)Status.PaperInfoStatus.Using && p.PrinterID ==printerid);
            //    PaperInfoModel paperInfo = MappingPaperToModel(paper);
            //    return paperInfo;
            //}
            string QueryPaper = "select AddTime,EndPaperLength,LastBordId,LastLength,LastPrintTime,Length,paperface.PaperFaceID,PaperFaceName,papertype.PaperTypeID,PaperTypeName,PrinterID,PrintPaperId,Remark,Status,InvalidationArea,ValidationArea,PaperConsumptionLength,Width from paperinfo,papertype,paperface where PrinterID=" + printerid + " and status=" + (int)Status.PaperInfoStatus.Using + " and  papertype.papertypeid=paperinfo.papertypeid and paperface.paperfaceid=paperinfo.paperfaceid";
            DataTable table = _OperateDb.QueryDataTable(QueryPaper);
            if (table.Rows.Count > 0)
            {
                return MappingPaperToModel(table.Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 校正纸长
        /// </summary>
        /// <param name="printer"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public int CalibrationPaperLength(Printer_info printer, double length, bool isAdd)
        {
            int ret = 0;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperInfo paper = entities.PaperInfo.FirstOrDefault(p => p.Status == (int)Status.PaperInfoStatus.Using && p.PrinterID == printer.PrinterID);
            //        if (isAdd)
            //        {
            //            paper.Length += length;
            //            paper.LastLength += length;
            //        }
            //        else
            //        {
            //            if (paper.LastLength >= length)
            //            {
            //                paper.Length -= length;
            //                paper.LastLength -= length;
            //            }
            //            else
            //            {
            //                ret = -2;
            //            }
            //        }
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            string QueryLastLength = "select LastLength from PaperInfo where status=" + (int)Status.PaperInfoStatus.Using + " and PrinterID=" + printer.PrinterID;
            double LastLength = (double)_OperateDb.ExecuteScalar(QueryLastLength);
            string AddSql = "";
            if (isAdd)
            {
                AddSql = "update board set Length=Length+" + length + ",LastLength=LastLength+" + length + "where status=" + (int)Status.PaperInfoStatus.Using + " and PrinterID=" + printer.PrinterID;
            }
            else
            {
                length = -length;
                AddSql = "update board set Length=Length+" + length + ",LastLength=LastLength+" + length + "where status=" + (int)Status.PaperInfoStatus.Using + " and PrinterID=" + printer.PrinterID;

            }
            ret = AccessHelper.ExecuteNonQuery(AddSql);
            return ret;

        }
        /// <summary>
        /// 获取一台打印机的所有打印纸
        /// </summary>
        /// <param name="printerId"></param>
        /// <returns></returns>
        public List<PaperInfoModel> GetPaperInfoList(int printerId)
        {
            List<PaperInfoModel> list = new List<PaperInfoModel>();
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    List<PaperInfo> paperlist = entities.PaperInfo.Where(p => p.PrinterID == printerId).ToList();
            //    foreach (var item in paperlist)
            //    {
            //        PaperInfoModel paperInfo = new PaperInfoModel();

            //        paperInfo.PrintPaperId = item.PrintPaperId;
            //        paperInfo.PrinterID = item.PrinterID;
            //        paperInfo.AddTime = item.AddTime;
            //        paperInfo.LastLength = item.LastLength;
            //        paperInfo.LastPrintTime = item.LastPrintTime;
            //        paperInfo.Length = item.Length;
            //        paperInfo.Width = item.Width;
            //        paperInfo.EndPaperLength = item.EndPaperLength;
            //        if (string.IsNullOrEmpty(item.Remark))
            //        {
            //            paperInfo.Remark = item.Remark;
            //        }
            //        else
            //        {
            //            paperInfo.Remark = string.Empty;
            //        }
            //        paperInfo.Status = item.Status;

            //        if (!item.PaperTypeReference.IsLoaded)
            //        {
            //            item.PaperTypeReference.Load();
            //        }
            //        if (!item.PaperFaceReference.IsLoaded)
            //        {
            //            item.PaperFaceReference.Load();
            //        }
            //        paperInfo.PaperFaceName = item.PaperFace.PaperFaceName;
            //        paperInfo.PaperTypeName = item.PaperType.PaperTypeName;
            //        list.Add(paperInfo);
            //    }
            //}
            string QueryPaper = "select AddTime,EndPaperLength,LastBordId,LastLength,LastPrintTime,Length,paperface.PaperFaceID,PaperFaceName,papertype.PaperTypeID,PaperTypeName,PrinterID,PrintPaperId,Remark,Status,InvalidationArea,ValidationArea,PaperConsumptionLength,Width from papernfo,papertype,paperface where PrinterID=" + printerId + " and status=" + (int)Status.PaperInfoStatus.Using + " papertype.papertypeid=paperinfo.papertypeid and paperface.paperfaceid=paperinfo.paperfaceid";
            DataTable table = _OperateDb.QueryDataTable(QueryPaper);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(MappingPaperToModel(_OperateDb.QueryDataTable(QueryPaper).Rows[i]));
            }

            return list;
        }
        private PaperInfoModel MappingPaperToModel(DataRow paper)
        {
            PaperInfoModel paperInfo = new PaperInfoModel();
            paperInfo.AddTime = Convert.ToDateTime(paper["AddTime"]);
            paperInfo.EndPaperLength = Convert.ToDouble(paper["EndPaperLength"]);
            paperInfo.LastBordId = Convert.ToInt32(paper["LastBordId"]);
            paperInfo.LastLength = Convert.ToDouble(paper["LastLength"]);
            paperInfo.LastPrintTime = Convert.ToDateTime(paper["LastPrintTime"]); ;
            paperInfo.Length = Convert.ToDouble(paper["Length"]);
            paperInfo.PaperFaceID = Convert.ToInt32(paper["PaperFaceID"]==null?0:paper["PaperFaceID"]);
            paperInfo.PaperFaceName = Convert.ToString(paper["PaperFaceName"]);
            paperInfo.PaperTypeID = Convert.ToInt32(paper["PaperTypeID"]==null?0:paper["PaperTypeID"]);
            paperInfo.PaperTypeName = Convert.ToString(paper["PaperTypeName"]);
            paperInfo.PrinterID = Convert.ToInt32(paper["PrinterID"]); ;
            paperInfo.PrintPaperId = Convert.ToInt32(paper["PrintPaperId"]); ;
            paperInfo.Remark = Convert.ToString(paper["Remark"]);
            paperInfo.Status = Convert.ToInt32(paper["Status"]); ;
            paperInfo.InvalidationArea = Convert.ToDouble(paper["InvalidationArea"]);
            paperInfo.ValidationArea = Convert.ToDouble(paper["ValidationArea"]);
            paperInfo.PaperConsumptionLength = Convert.ToDouble(paper["PaperConsumptionLength"]);

            paperInfo.Width = Convert.ToDouble(paper["Width"]);
            return paperInfo;
        }
        //private PaperInfoModel MappingPaperToModel(PaperInfo paper)
        //{
        //    PaperInfoModel paperInfo = new PaperInfoModel();
        //    if (paper != null)
        //    {
        //        if (!paper.PaperFaceReference.IsLoaded)
        //        {
        //            paper.PaperFaceReference.Load();
        //        }
        //        if (!paper.PaperTypeReference.IsLoaded)
        //        {
        //            paper.PaperTypeReference.Load();
        //        }
        //        paperInfo.AddTime = paper.AddTime;
        //        paperInfo.EndPaperLength = paper.EndPaperLength;
        //        paperInfo.LastBordId = paper.LastBordId;
        //        paperInfo.LastLength = paper.LastLength;
        //        paperInfo.LastPrintTime = paper.LastPrintTime;
        //        paperInfo.Length = paper.Length;
        //        paperInfo.PaperFaceID = paper.PaperFace.PaperFaceId;
        //        paperInfo.PaperFaceName = paper.PaperFace.PaperFaceName;
        //        paperInfo.PaperTypeID = paper.PaperType.PaperTypeId;
        //        paperInfo.PaperTypeName = paper.PaperType.PaperTypeName;
        //        paperInfo.PrinterID = paper.PrinterID;
        //        paperInfo.PrintPaperId = paper.PrintPaperId;
        //        paperInfo.Remark = paper.Remark;
        //        paperInfo.Status = paper.Status;
        //        paperInfo.InvalidationArea = paper.InvalidationArea;
        //        paperInfo.ValidationArea = paper.ValidationArea;
        //        paperInfo.PaperConsumptionLength = paper.PaperConsumptionLength;

        //        paperInfo.Width = paper.Width;
        //    }
        //    else
        //    {
        //        paperInfo = null;
        //    }
        //    return paperInfo;
        //}

        /// <summary>
        /// 换纸
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int ChangePaper(PaperInfoModel paperInfo, Printer_info printerInfo, out PaperInfoModel prePaperInfo)
        {
            prePaperInfo = new PaperInfoModel();
            //paperWasteRecordsId = 0;
            int ret = 0;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperInfo prePaper = entities.PaperInfo.FirstOrDefault(p => p.Status ==(int) Status.PaperInfoStatus.Using && p.PrinterID ==printerInfo.PrinterID);
            //        if (prePaper != null)
            //        {
            //            prePaper.Status = (int)Status.PaperInfoStatus.Used;
            //            prePaperInfo.Length = prePaper.Length;
            //            prePaperInfo.LastLength = prePaper.LastLength;
            //            prePaperInfo.PrintPaperId = prePaper.PrintPaperId;
            //            prePaperInfo.Width = prePaper.Width;
            //            prePaperInfo.LastPrintTime = prePaper.LastPrintTime;
            //            prePaperInfo.ValidationArea = prePaper.ValidationArea;
            //            prePaperInfo.InvalidationArea = prePaper.InvalidationArea;
            //        }
            //        else
            //        {
            //            prePaperInfo = null;
            //        }
            //        PaperInfo paper = entities.PaperInfo.FirstOrDefault(p => p.PrintPaperId == paperInfo.PrintPaperId);
            //        paper.Status = (int)Status.PaperInfoStatus.Using;
            //        paper.LastLength = paperInfo.LastLength;
            //        paper.PrinterID = printerInfo.PrinterID;

            //        //记录纸头
            //        //SmallERP.DAL.PaperWasteType wasteType = entities.PaperWasteType.FirstOrDefault(p=>p.PaperOperationTypeName =="纸头");
            //        //PaperWasteRecords wasteRecord = new PaperWasteRecords();
            //        //wasteRecord.CreateTime = UnityMethod.GetSystemTime();
            //        //wasteRecord.Length = paperTitle;
            //        //wasteRecord.PaperInfo = paper;
            //        //wasteRecord.Status = 0;
            //        //wasteRecord.Width = paper.Width;
            //        //wasteRecord.PaperWasteType = wasteType;
            //        //entities.AddToPaperWasteRecords(wasteRecord);
            //        entities.SaveChanges();


            //        //paperWasteRecordsId = wasteRecord.Id;
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}

            ///原来的纸标记成部分使用
            DataTable table = _OperateDb.ChangerPaper(printerInfo.PrinterID, paperInfo.PrintPaperId);
            if (table != null)
            {
                prePaperInfo.Length = Convert.ToDouble(table.Rows[0]["Length"]);
                prePaperInfo.LastLength = Convert.ToDouble(table.Rows[0]["LastLength"]);
                prePaperInfo.PrintPaperId = Convert.ToInt32(table.Rows[0]["PrintPaperId"]);
                prePaperInfo.Width = Convert.ToDouble(table.Rows[0]["Width"]); ;
                prePaperInfo.LastPrintTime = Convert.ToDateTime(table.Rows[0]["LastPrintTime"]);
                prePaperInfo.ValidationArea = Convert.ToDouble(table.Rows[0]["ValidationArea"]); ;
                prePaperInfo.InvalidationArea = Convert.ToDouble(table.Rows[0]["InvalidationArea"]);
            }
            else
            {
                ret = -1;
            }
            return ret;
        }

        #region IDataAccess 成员
        /// <summary>
        /// 添加纸
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Insert(object o)
        {
            int ret = 0;
            PaperInfoModel paperInfo = (PaperInfoModel)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperInfo paper = new PaperInfo();
            //        paper.AddTime = paperInfo.AddTime;
            //        paper.EndPaperLength = paperInfo.EndPaperLength;
            //        paper.InvalidationArea = paperInfo.InvalidationArea;
            //        paper.LastBordId = paperInfo.LastBordId;
            //        paper.LastLength = paperInfo.LastLength;
            //        paper.LastPrintTime = paperInfo.LastPrintTime;
            //        paper.Length = paperInfo.Length;
            //        paper.PaperConsumptionLength = paperInfo.PaperConsumptionLength;
            //        paper.PaperEndArea = paperInfo.PaperEndArea;
            //        paper.PaperFace = entities.PaperFace.FirstOrDefault(p => p.PaperFaceId == paperInfo.PaperFaceID);
            //        paper.PaperTitleArea = paperInfo.PaperTitleArea;
            //        paper.PaperType = entities.PaperType.FirstOrDefault(p => p.PaperTypeId == paperInfo.PaperTypeID);
            //        paper.PrinterID = paperInfo.PrinterID;
            //        paper.Remark = paperInfo.Remark;
            //        paper.Status = paperInfo.Status;
            //        paper.ValidationArea = paperInfo.ValidationArea;
            //        paper.Width = paperInfo.Width;
            //        entities.AddToPaperInfo(paper);
            //        entities.SaveChanges();
            //    }
            //    catch(Exception ex)
            //    {
            //        ret = -1;
            //    }
            //}

            string insertPaperinfo = "insert into PaperInfo(AddTime,EndPaperLength,InvalidationArea,LastBordId,LastLength,LastPrintTime,Length,PaperConsumptionLength,PaperEndArea,PaperFaceId,PaperTitleArea,PaperTypeId,PrinterID,Remark,Status,ValidationAreaWidth) values("
                                       + "'" + paperInfo.AddTime + "'," + paperInfo.EndPaperLength + "," + paperInfo.InvalidationArea + "," + paperInfo.LastBordId + ","
            + paperInfo.LastLength + ",'" + paperInfo.LastPrintTime + "'" + paperInfo.Length + "," + paperInfo.PaperConsumptionLength + "," + paperInfo.PaperEndArea + "," + paperInfo.PaperFaceID + "," + paperInfo.PaperTypeID + "," + paperInfo.PrinterID
            + "'" + paperInfo.Remark + "'," + paperInfo.Status + "," + paperInfo.ValidationArea + "," + paperInfo.Width + ")";
            int istVal = _OperateDb.ExecuteNonQuery(insertPaperinfo);
            if (istVal == 0)
            {
                ret = -1;

            } return ret;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Update(object o)
        {

            int ret = 0;
            PaperInfoModel paperInfo = (PaperInfoModel)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperInfo paper = entities.PaperInfo.FirstOrDefault(p => p.PrintPaperId == paperInfo.PrintPaperId);
            //        paper.LastPrintTime = paperInfo.LastPrintTime;
            //        paper.Length = paperInfo.Length;
            //        paper.LastBordId = paperInfo.LastBordId;
            //        paper.LastLength = paperInfo.LastLength;
            //        paper.Status = paperInfo.Status;
            //        paper.PaperConsumptionLength = paperInfo.PaperConsumptionLength;
            //        paper.ValidationArea = paperInfo.ValidationArea;
            //        paper.InvalidationArea = paperInfo.InvalidationArea;
            //        entities.SaveChanges();
            //    }
            //    catch (Exception ex)
            //    {
            //        ret = -1;
            //    }
            //}
            string upPaperinfo = "update PaperInfo set LastPrintTime='" + paperInfo.LastPrintTime + "',Length=" + paperInfo.Length + ",LastBordId="
                + paperInfo.LastBordId + ",LastLength=" + paperInfo.LastLength + ",Status=" + paperInfo.Status + ",PaperConsumptionLength=" + paperInfo.PaperConsumptionLength
                + ",ValidationArea=" + paperInfo.ValidationArea + ",InvalidationArea=" + paperInfo.InvalidationArea + " where PrintPaperId=" + paperInfo.PrintPaperId;
            if (_OperateDb.ExecuteNonQuery(upPaperinfo) == 0)
            {
                ret = -1;
            }
            return ret;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int SetPaperComplete(object o)
        {

            int ret = 0;
            PaperInfoModel paperInfo = (PaperInfoModel)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperInfo paper = entities.PaperInfo.FirstOrDefault(p => p.PrintPaperId == paperInfo.PrintPaperId);
            //        paper.LastPrintTime = paperInfo.LastPrintTime;
            //        paper.Length = paperInfo.Length;
            //        paper.LastBordId = paperInfo.LastBordId;
            //        paper.LastLength = paperInfo.LastLength;
            //        paper.Status = paperInfo.Status;
            //        paper.PaperConsumptionLength = paperInfo.PaperConsumptionLength;
            //        paper.ValidationArea = paperInfo.ValidationArea;
            //        paper.InvalidationArea = paperInfo.InvalidationArea;
            //        entities.SaveChanges();
            //    }
            //    catch (Exception ex)
            //    {
            //        ret = -1;
            //    }
            //}
            string upPaperinfo = "update PaperInfo set LastPrintTime='" + paperInfo.LastPrintTime + "',Length=" + paperInfo.Length + ",LastBordId="
                + paperInfo.LastBordId + ",LastLength" + paperInfo.LastLength + ",Status=" + paperInfo.Status + ",PaperConsumptionLength=" + paperInfo.PaperConsumptionLength
                + ",ValidationArea=" + paperInfo.ValidationArea + ",InvalidationArea=" + paperInfo.InvalidationArea + " where PrintPaperId=" + paperInfo.PrintPaperId;
            if (_OperateDb.ExecuteNonQuery(upPaperinfo) == 0)
            {
                ret = -1;
            }
            return ret;
        }
        public int Delete(object o)
        {
            throw new NotImplementedException();
        }

        #endregion


    }
}
